package com.knowledge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.knowledge.common.enums.CategoryTypeEnum;
import com.knowledge.common.enums.UserRoleEnum;
import com.knowledge.dao.entity.Category;
import com.knowledge.dao.mapper.CategoryMapper;
import com.knowledge.service.CategoryService;
import com.knowledge.vo.CategoryVo;
import com.knowledge.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;



/**
 * @author zzz
 * @create 2022-02-11 19:23
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public CategoryVo findCategoryById(Long categoryId) {
        Category category = categoryMapper.selectById(categoryId);
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category,categoryVo);
        categoryVo.setId(String.valueOf(category.getId()));
        return categoryVo;
    }

    @Override
    public Result findAll() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Category::getId,Category::getCategoryName);
        List<Category> categories = categoryMapper.selectList(queryWrapper);
        //我们根据接口要求返回CategoryVo 所以这里需要转换
        return Result.success(copyList(categories));
    }

    @Override
    public Result findAllDetail() {
        List<Category> categories = categoryMapper.selectList(new LambdaQueryWrapper<>());
        return Result.success(copyList(categories));
    }

    @Override
    public Result findCategoriesDetailById(Long id) {
//        Category category = categoryMapper.selectById(id);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getId,id);
        Category category = categoryMapper.selectOne(queryWrapper);
        //返回给前端数据
        CategoryVo categoryVo = copy(category);
        return Result.success(category);
    }

    private List<CategoryVo> copyList(List<Category> categories) {
        List<CategoryVo> categoryVos = new ArrayList<>();
        for (Category category : categories) {
            categoryVos.add(copy(category));
        }
        return categoryVos;
    }

    private CategoryVo copy(Category category) {
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category,categoryVo);
        categoryVo.setId(String.valueOf(category.getId()));
        return categoryVo;
    }

    @Override
    public Long getOrSaveCategory(Integer admin) {

        LambdaQueryWrapper<Category> query = Wrappers.lambdaQuery();

        CategoryTypeEnum typeEnum;

        if (UserRoleEnum.ADMIN.getType().equals(admin)) {
            typeEnum = CategoryTypeEnum.OFFICIAL;
        }else {
            typeEnum = CategoryTypeEnum.NORMAL;
        }
        query.eq(Category::getCategoryCode, typeEnum.getCode());
        Category category = categoryMapper.selectOne(query);

        if (category == null) {
            category = new Category();
            category.setCategoryCode(typeEnum.getCode());
            category.setCategoryName(typeEnum.getDesc());
            category.setDescription(typeEnum.getDetail());
            category.setAvatar(typeEnum.getUrl());

            categoryMapper.insert(category);
        }
        return category.getId();
    }
}
